﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public class PingResultCollection
{
    public const int maxPingSaveCount = 8;

    /// <summary>
    /// 最近100帧的ping结果
    /// </summary>
    public List<int> pingList = new List<int>();
    public List<int> delayList = new List<int>();
    public List<long> deltaList = new List<long>();

    /// <summary>
    /// 平均ping结果
    /// </summary>
    public PingResult averagePingResult
    {
        get
        {
            if(pingList.Count == 0)
                return new PingResult(0, 0, 0); 

            int ping = (int)pingList.Average();
            int delay = (int)delayList.Average();
            long delta = (long)deltaList.Average();

            //GameDebug.LogShowy($"delta:{delta} list:{deltaList.GetListStr()}");
            return new PingResult(ping, delay, delta);
        }
    }

    public void AddPingResult(PingResult pingResult)
    {
        pingList.Add(pingResult.ping);
        delayList.Add(pingResult.delay);
        deltaList.Add(pingResult.delta);

        if (pingList.Count > maxPingSaveCount)
        {
            pingList.RemoveAt(0);
            delayList.RemoveAt(0);
            deltaList.RemoveAt(0);
        }
    }
}

public struct PingResult
{
    /// <summary>
    /// ping值
    /// </summary>
    public int ping;

    /// <summary>
    /// 网络延迟
    /// </summary>
    public int delay;

    /// <summary>
    /// 与服务器时间差
    /// </summary>
    public long delta;

    public long serverTicks => CommonFunctions.GetTimeStamp() + (long)delta;

    public PingResult(int _ping, int _delay, long _delta)
    {
        this.ping = _ping;
        this.delay = _delay;
        this.delta = _delta;
    }

    public PingResult(long t1,long t2,long t3,long t4)
    {
        /////////四点计算
        ///server       t2    t3      
        ///client  t1               t4
        ////////
        this.ping = (int)(t4 - t1);
        this.delay = (int) (((t4 - t1) - (t3 - t2))/2);
        this.delta = (((t2 - delay) - t1) + ((t3 + delay) - t4 )) / 2;

        //GameDebug.LogShowy($"ping  t1:{t1}  t2:{t2}    t3:{t3}   t4:{t4}  {t4 - t1}  ping:{(t4 - t1) / 10000}  delta:{(t2 - delay) - t1}   delay：{delay}");
    }
}


public partial class Request_Ping : RequestBase
{
    public Request_Ping() : base()
    {
        this.MessagePacket.key = "NetRequest_Ping";
    }

    protected override ISerialize requestData
    {
        get
        {
            return pingData;
        }
    }

    public PingRequest pingData;

    public override ResponseData NewResponseData() => new PingResponse();

    //public override ResponseBase GetResponseHandler()
    //{
    //    return new Response_Ping();
    //}

}



//public partial class Response_Ping  : ResponseBase
//{
//    public PingResponse pingResponseData => this.responseData as PingResponse;

//    public override ResponseData NewResponseData ()=> new PingResponse();
        
    

//    public override void Excuate()
//    {
//        Request_Ping basePingRequest_ = this.request as Request_Ping;

//        long t1 = basePingRequest_.pingData.clientSendTime;
//        long t2 = pingResponseData.serverReceiveTime;
//        long t3 = this.MessagePacketFrimServer.time;
//        long t4 = CommonFunctions.GetTimeStamp();

//        GameDebug.Log($"Response_Ping  Excute  t1:{t1}  t2:{t2}  t3:{t3}  t4:{t4}");

//        if (this.request.clientSocket != null )
//        {
//            this.request.clientSocket.pingResultCollection.AddPingResult(new PingResult(t1, t2, t3, t4));
//        }
//    }
//}
